﻿namespace BM.Tools.WebControls.MForm.Design
{
    using System;
    using System.Web.UI;
    using System.Windows.Forms;

    /// <summary>
    /// This form can be used to set css style to a control
    /// </summary>
    public partial class StyleCollectionEditor : Form
    {        
        /// <summary>
        /// Initializes a new instance of the <see cref="StyleCollectionEditor"/> class. 
        /// </summary>
        /// <param name="collection">
        /// The collection.
        /// </param>
        public StyleCollectionEditor(CssStyleCollection collection)
        {
            Collection = collection;
            InitializeComponent();
        }

        /// <summary>
        /// Gets the css style collection.
        /// </summary>
        public CssStyleCollection Collection
        {
            get;
            private set;
        }

        /// <summary>
        /// Updates the css style collection according to the contents of the style collection grid
        /// </summary>        
        public void Synchronize()
        {
            Collection.Clear();
            foreach (DataGridViewRow row in StyleCollectionGrid.Rows)
            {
                if (row.Cells["KeyColumn"].Value != null)
                {
                    Collection[row.Cells["KeyColumn"].Value.ToString()] = row.Cells["ValueColumn"].Value.ToString();
                }
            }
        }

        /// <summary>
        /// Fills the style collection grid with the values from the css style collection
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The event args.
        /// </param>
        private void StyleCollectionEditor_Load(object sender, EventArgs e)
        {
            foreach (string key in Collection.Keys)
            {
                StyleCollectionGrid.Rows.Add(key, Collection[key]);
            }
        }
    }
}